import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Layout from '@/views/layout/Layout'

Vue.use(VueRouter)

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/user/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/user/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/home/index.vue')
      },
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'courses',
        name: 'Courses',
        component: () => import('@/views/course/index.vue'),
        meta: { title: '创客工坊' }
      },
      {
        path: 'ppt-center',
        name: 'PptCenter',
        component: () => import('@/views/ppt-center/index.vue'),
        meta: { title: 'PPT课件中心' }
      },
      {
        path: 'ppt-center/:id',
        name: 'PptDetail',
        component: () => import('@/views/ppt-center/Detail.vue'),
        meta: { title: 'PPT课件详情' }
      },
      {
        path: 'video-center',
        name: 'VideoCenter',
        component: () => import('@/views/video-center/index.vue'),
        meta: { title: '视频课件中心' }
      },
      {
        path: 'video-center/:id',
        name: 'VideoDetail',
        component: () => import('@/views/video-center/Detail.vue'),
        meta: { title: '视频课件详情' }
      },
      {
        path: 'course/:id',
        name: 'CourseDetail',
        component: () => import('@/views/course/Detail.vue'),
        meta: { title: '课程详情' }
      },
      {
        path: 'creator-support',
        name: 'CreatorSupport',
        component: () => import('@/views/creator-support/index.vue'),
        meta: { title: '创作者支持' }
      },
      {
        path: 'creator-support/apply',
        name: 'CreatorApply',
        component: () => import('@/views/creator-support/Apply.vue'),
        meta: { title: '申请成为创作者' }
      },
      {
        path: 'creator-support/resources',
        name: 'CreatorResources',
        component: () => import('@/views/creator-support/Resources.vue'),
        meta: { title: '创作者资源' }
      },
      {
        path: 'teaching-tools',
        name: 'TeachingTools',
        component: () => import('@/views/teaching-tools/index.vue'),
        meta: { title: '教具库' }
      },
      {
        path: 'materials',
        name: 'Materials',
        component: () => import('@/views/material/index.vue'),
        meta: { title: '学习资料' }
      },
      {
        path: 'material/:id',
        name: 'MaterialDetail',
        component: () => import('@/views/material/Detail.vue'),
        meta: { title: '资料详情' }
      },
      {
        path: 'exams',
        name: 'Exams',
        component: () => import('@/views/exam/index.vue'),
        meta: { title: '我的考试' }
      },
      {
        path: 'exam/:id',
        name: 'ExamDetail',
        component: () => import('@/views/exam/Detail.vue'),
        meta: { title: '考试详情' }
      },
      {
        path: 'exam/take/:id',
        name: 'TakeExam',
        component: () => import('@/views/exam/Take.vue'),
        meta: { title: '参加考试' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/user/Profile.vue'),
        meta: { title: '个人中心' }
      },
      {
        path: 'teacher-center',
        name: 'TeacherCenter',
        component: () => import('@/views/teacher-center/index.vue'),
        meta: { title: '教师交流中心' }
      },
      {
        path: 'teacher-center/:id',
        name: 'TeacherDetail',
        component: () => import('@/views/teacher-center/Detail.vue'),
        meta: { title: '教师详情' }
      }
    ]
  },
  {
    path: '/ai-assistant',
    component: Layout,
    children: [
      {
        path: '',
        name: 'AIAssistant',
        component: () => import('@/views/ai-assistant/index.vue'),
        meta: { title: 'AI学习助手' }
      }
    ]
  },
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 教育平台学生端` : '教育平台学生端'
  
  // 检查是否需要登录
  const token = localStorage.getItem('token')
  
  if (to.path !== '/login' && to.path !== '/register' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 